﻿module UI {

    export class InputHelper {

        public canvas: HTMLCanvasElement = null;

        constructor(canvas: HTMLCanvasElement) {
            this.canvas = canvas;
            window.onresize = () => { this.handleWindowResizedInternal() };
            canvas.onmousedown = (e) => { this.handleMouseDownInternal(e) };
            canvas.onmousemove = (e) => { this.handleMouseMoveInternal(e) };
            canvas.onmouseup = (e) => { this.handleMouseUpInternal(e) };
            canvas.onclick = (e) => { this.handleLeftClickInternal(e) };
            canvas.oncontextmenu = (e) => { this.handleRightClickInternal(e) };
            document.onmousewheel = (e) => { this.handleMouseWheelInternal(e) }; // IE
            document.addEventListener("DOMMouseScroll", (e: MouseWheelEvent) => { this.handleMouseWheelInternal(e) }, false); // Firefox
            document.onkeydown = (e) => { this.handleKeyDownInternal(e) };
            document.onkeyup = (e) => { this.handleKeyUpInternal(e) };
            document.onkeypress = (e) => { this.handleKeyPressInternal(e) };
        }

        public handleMouseDown: (e: MouseEvent) => void = undefined;

        private handleMouseDownInternal(e: MouseEvent) {
            var control = UI.getControlByPosition(UI.root, e.pageX, e.pageY);
            if (control !== null) {
                UI.handleMouseDown(e.pageX, e.pageY);
            } else if (e.button == 0) {
                if (this.handleMouseDown !== undefined && this.handleMouseDown != null) {
                    this.handleMouseDown(e);
                }
            }
        }

        public handleMouseMove: (e: MouseEvent) => void = undefined;

        private handleMouseMoveInternal(e: MouseEvent) {
            UI.handleMouseMove(e.pageX, e.pageY);
            if (this.handleMouseMove !== undefined && this.handleMouseMove != null) {
                this.handleMouseMove(e);
            }
        }

        public handleMouseUp: (e: MouseEvent) => void = undefined;

        private handleMouseUpInternal(e: MouseEvent) {
            var control = UI.getControlByPosition(UI.root, e.pageX, e.pageY);
            if (control !== null) {
                UI.handleMouseUp(e.pageX, e.pageY);
            } else if (e.button == 0) {
                if (this.handleMouseUp !== undefined && this.handleMouseUp != null) {
                    this.handleMouseUp(e);
                }
            }
        }

        public handleLeftClick: (e: MouseEvent) => void = undefined;

        private handleLeftClickInternal(e: MouseEvent) {
            var control = UI.getControlByPosition(UI.root, e.pageX, e.pageY);
            if (control !== null) {
                UI.handleClick(e.pageX, e.pageY);
            } else {
                if (this.handleLeftClick !== undefined && this.handleLeftClick != null) {
                    this.handleLeftClick(e);
                }
            }
        }

        public handleRightClick: (e: MouseEvent) => void = undefined;

        private handleRightClickInternal(e: MouseEvent) {
            var control = UI.getControlByPosition(UI.root, e.pageX, e.pageY);
            if (control !== null) {
                UI.handleClick(e.pageX, e.pageY);
            } else {
                if (this.handleRightClick !== undefined && this.handleRightClick != null) {
                    this.handleRightClick(e);
                }
            }
            return false;
        }

        public handleMouseWheel: (delta: number) => void = undefined;

        private handleMouseWheelInternal(e: MouseWheelEvent) {
            var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
            if (this.handleMouseWheel !== undefined && this.handleMouseWheel != null) {
                this.handleMouseWheel(delta);
            }
            e.preventDefault();
        }

        public handleKeyDown: (e: KeyboardEvent) => void = undefined;

        private handleKeyDownInternal(e: KeyboardEvent) {
            if (!UI.handleKeyDown(e)) {
                if (this.handleKeyDown !== undefined && this.handleKeyDown != null) {
                    this.handleKeyDown(e);
                }
            }
            var key = UI.getKey(e);
            if (key == 'Backspace') e.preventDefault();
        }

        public handleKeyUp: (e: KeyboardEvent) => void = undefined;

        private handleKeyUpInternal(e: KeyboardEvent) {
            if (!UI.handleKeyUp(e)) {
                if (this.handleKeyUp !== undefined && this.handleKeyUp != null) {
                    this.handleKeyUp(e);
                }
            }
            var key = UI.getKey(e);
            if (key == 'Backspace') e.preventDefault();
        }

        public handleKeyPress: (e: KeyboardEvent) => void = undefined;

        private handleKeyPressInternal(e: KeyboardEvent) {
            if (!UI.handleKeyPress(e)) {
                if (this.handleKeyPress !== undefined && this.handleKeyPress != null) {
                    this.handleKeyPress(e);
                }
            }
            var key = UI.getKey(e);
            if (key == 'Backspace') e.preventDefault();
        }

        public handleWindowResized: () => void = undefined;

        private handleWindowResizedInternal() {
            if (this.handleWindowResized !== undefined && this.handleWindowResized != null) {
                this.handleWindowResized();
            }
        }

    }

} 